package com.airnews.ticket.ui.flight;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.airnews.ticket.R;
import com.airnews.ticket.model.FlightInfo;
import com.airnews.ticket.model.Passenger;
import com.airnews.ticket.model.SearchFlight;
import com.airnews.ticket.sql.FavoriteInfoDB;
import com.airnews.ticket.ui.BaseActivity;
import com.airnews.ticket.util.Utils;
import com.mobclick.android.MobclickAgent;

public class MileagerInfoList extends BaseActivity implements
		AdapterView.OnItemClickListener {

	private final static int DELETE_DIALOG = 1;
	public static List<Map<String,String>> mileagerInfoList = new ArrayList<Map<String,String>>();
	public static Passenger passenger;//上一页的登机人信息
	public static FlightInfo flight;//去程
	public static FlightInfo returnFlight;//回程
	public static SearchFlight searchFlight;
	public static String fromAction;//来自那个action
	private Button backBut;
	public SimpleAdapter sa;
	public ListView lv;
	public Button mBtnBack,mBtnNext,passengerSaveBut;
	public List<Map<String, String>> data = new ArrayList<Map<String, String>>();

	private ProgressDialog progressDialog = null;
	
	private static int position;
	private TextView no_mileager_alert ;
	
    //登机人,证件号码,手机号码
    private EditText cnName,idNumber,mobile;

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mileagerinfo_list);
		// 删除窗口背景
        getWindow().setBackgroundDrawable(null);
		activityList.add(this);
		
		mileagerInfoList = FavoriteInfoDB.selectMileagerInfoList(MileagerInfoList.this);
		
		Log.i("mileagerInfoList size ", String.valueOf(mileagerInfoList.size()));
		
		sa = new SimpleAdapter(
				this,
				mileagerInfoList,
				R.layout.flight_mileager_item,
				new String[] { "mileagerCardAirline", "mileagerCardNumber"},
				new int[] { R.id.mileagerCardAirline, R.id.mileagerCardNumber}); //name<=>psgName,paperType<=>idType,idNo<=>paperNo
		lv = (ListView) findViewById(R.id.mileager_list);
		lv.setAdapter(sa);
		lv.setOnItemClickListener(this);
		
		no_mileager_alert = (TextView)findViewById(R.id.no_mileager_alert);
		if(mileagerInfoList.size() != 0) {
			no_mileager_alert.setVisibility(View.GONE);
			Utils.setListViewHeightBasedOnChildren(lv);//更新list高度
		} else {
			no_mileager_alert.setVisibility(View.VISIBLE);
			no_mileager_alert.setText("暂未添加登机人 ！");
		}
		
		backBut = (Button)findViewById(R.id.backBut);
		backBut.setOnClickListener(b_ocl);
	}
	
	OnClickListener b_ocl= new OnClickListener() { 
    	public void onClick(View v) { 
    		switch (v.getId()) {
	    		case R.id.backBut:
	    			Intent intent3 = new Intent();
	    			if(fromAction.equals("ADD")){
	    				intent3.setClass(MileagerInfoList.this, PassengerAdd.class);
	    				PassengerAdd.flight = flight;
		    			PassengerAdd.returnFlight = returnFlight;
		    			PassengerAdd.searchFlight = searchFlight;
	    			} else {
	    				intent3.setClass(MileagerInfoList.this, PassengerEdit.class);
	    				PassengerEdit.passenger = passenger;
	    				PassengerEdit.flight = flight;
	    				PassengerEdit.returnFlight = returnFlight;
	    				PassengerEdit.searchFlight = searchFlight;
	    			}
	    			
	    			startActivity(intent3);
	    			break;
    		}
    	}
	};
	public void onItemClick(AdapterView<?> arg0, View v, int position,
			long arg3) {
		switch (arg0.getId()) {
			case R.id.mileager_list:
				this.position = position;
				showDialog(DELETE_DIALOG);
				break;
		}
	}
	
	public Dialog onCreateDialog(int id) {
		switch (id) {
			case DELETE_DIALOG:
				return buildDeleteDialog();
		}
		return null;
	}

	private Dialog buildDeleteDialog() {
		 return new AlertDialog.Builder(this)
         .setTitle("请选择")
         .setItems(R.array.dialog_passenger_list_f, new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int which) {
            	 if(which==0){
		    			Map<String ,String> removeMap = mileagerInfoList.get(position);
		    			if(fromAction.equals("ADD")){
//		    				for (int i = 0; i < PassengerAdd.mileagerInfoList.size(); i++) {
//			    				Map m = PassengerAdd.mileagerInfoList.get(i);
//			    				if (removeMap.get("mileagerCardAirline").equals(m.get("mileagerCardAirline"))
//			    						&& removeMap.get("mileagerCardNumber").equals(m.get("mileagerCardNumber"))) {
//			    					Toast.makeText(MileagerInfoList.this, "已添加该常旅卡信息", Toast.LENGTH_LONG).show();
//			    					return;
//			    				}
//			    			}
		    				List<Map<String,String>> newMileagerInfoList = new ArrayList<Map<String,String>>();
		    				newMileagerInfoList.add(removeMap);
	            		    
		    				for (int i = 0; i < newMileagerInfoList.size(); i++) {
			    				Map m = newMileagerInfoList.get(i);
			    				Log.i("mileagerCardAirline = ",(String)m.get("mileagerCardAirline"));
		    				}
		    					
			    			PassengerAdd.mileagerInfoList = newMileagerInfoList;
			    			Log.i("add newMileagerInfoList size", String.valueOf(newMileagerInfoList.size()));
			    			Intent intent = new Intent();
							intent.setClass(MileagerInfoList.this, PassengerAdd.class);
							Log.i("mileagerInfoList airline", String.valueOf(flight == null ? "is NULL" : flight.airline));

							PassengerAdd.flight = flight;
							PassengerAdd.returnFlight = returnFlight;
							PassengerAdd.searchFlight = searchFlight;
							PassengerAdd.passenger = passenger;
							startActivity(intent);
		    			} else {
//		    				for (int i = 0; i < PassengerEdit.mileagerInfoList.size(); i++) {
//			    				Map m = PassengerEdit.mileagerInfoList.get(i);
//			    				if (removeMap.get("mileagerCardAirline").equals(m.get("mileagerCardAirline"))
//			    						&& removeMap.get("mileagerCardNumber").equals(m.get("mileagerCardNumber"))) {
//			    					Toast.makeText(MileagerInfoList.this, "已添加该常旅卡信息", Toast.LENGTH_LONG).show();
//			    					return;
//			    				}
//			    			}
		    				
		    				List<Map<String,String>> newMileagerInfoList = new ArrayList<Map<String,String>>();
		    				newMileagerInfoList.add(removeMap);
	            		    
		    				for (int i = 0; i < newMileagerInfoList.size(); i++) {
			    				Map m = newMileagerInfoList.get(i);
			    				Log.i("edit mileagerCardAirline = ",(String)m.get("mileagerCardAirline"));
		    				}
		    				
		    				PassengerEdit.mileagerInfoList = newMileagerInfoList;
			    			
			    			Intent intent = new Intent();
							intent.setClass(MileagerInfoList.this, PassengerEdit.class);

							PassengerEdit.flight = flight;
							PassengerEdit.returnFlight = returnFlight;
							PassengerEdit.searchFlight = searchFlight;
							PassengerEdit.passenger = passenger;
							startActivity(intent);
		    			}
		    			
                  }
                 if(which==1){
                	Intent intenthistory = new Intent(MileagerInfoList.this,MileagerInfoEdit.class);
                  	intenthistory.putExtra("position", position);
                  	MileagerInfoEdit.flight = flight;
                  	MileagerInfoEdit.data = mileagerInfoList;
                  	MileagerInfoEdit.searchFlight = searchFlight;
                  	MileagerInfoEdit.passenger = passenger;
     				startActivity(intenthistory);
                 }else if(which==2){
                	Map<String ,String> removeMap = mileagerInfoList.remove(position);
             		if(mileagerInfoList.size() != 0) {
             			no_mileager_alert.setVisibility(View.GONE);
            			Utils.setListViewHeightBasedOnChildren(lv);
                    	sa.notifyDataSetChanged();
            		} else {
            			no_mileager_alert.setVisibility(View.VISIBLE);
            			lv.setVisibility(View.GONE);
            			no_mileager_alert.setText("暂未添加常旅卡 ！");
            		}
             		Log.i("removeMap mileagerCardAirline", removeMap.get("mileagerCardAirline"));
             		Log.i("removeMap mileagerCardNumber", removeMap.get("mileagerCardNumber"));
             		FavoriteInfoDB.deleteMileagerInfo(MileagerInfoList.this, removeMap.get("mileagerCardAirline"),removeMap.get("mileagerCardNumber"));
                 }
             }
         })
         .create();
	}
	
	public void onResume() {     
		super.onResume();     
		MobclickAgent.onResume(this); 
	}  
	
	public void onPause() {     
		super.onPause();     
		MobclickAgent.onPause(this); 
	}  

	
}
